/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/dingdan">dingdan</a> All rights reserved.
 */
package com.thinkgem.dingdan.modules.bu.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.dingdan.common.config.Global;
import com.thinkgem.dingdan.common.persistence.Page;
import com.thinkgem.dingdan.common.utils.StringUtils;
import com.thinkgem.dingdan.common.web.BaseController;
import com.thinkgem.dingdan.modules.bu.entity.BuOrderCp;
import com.thinkgem.dingdan.modules.bu.service.BuOrderCpService;

/**
 * 主子表生成Controller
 * @author ThinkGem
 * @version 2018-08-26
 */
@Controller
@RequestMapping(value = "${adminPath}/bu/buOrderCp")
public class BuOrderCpController extends BaseController {

	@Autowired
	private BuOrderCpService buOrderCpService;
	
	@ModelAttribute
	public BuOrderCp get(@RequestParam(required=false) String id) {
		BuOrderCp entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = buOrderCpService.get(id);
		}
		if (entity == null){
			entity = new BuOrderCp();
		}
		return entity;
	}
	
	@RequiresPermissions("bu:buOrderCp:view")
	@RequestMapping(value = {"list", ""})
	public String list(BuOrderCp buOrderCp, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BuOrderCp> page = buOrderCpService.findPage(new Page<BuOrderCp>(request, response), buOrderCp); 
		model.addAttribute("page", page);
		return "modules/bu/buOrderCpList";
	}

	@RequiresPermissions("bu:buOrderCp:view")
	@RequestMapping(value = "form")
	public String form(BuOrderCp buOrderCp, Model model) {
		model.addAttribute("buOrderCp", buOrderCp);
		return "modules/bu/buOrderCpForm";
	}

	@RequiresPermissions("bu:buOrderCp:edit")
	@RequestMapping(value = "save")
	public String save(BuOrderCp buOrderCp, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, buOrderCp)){
			return form(buOrderCp, model);
		}
		buOrderCpService.save(buOrderCp);
		addMessage(redirectAttributes, "保存主子表成功");
		return "redirect:"+Global.getAdminPath()+"/bu/buOrderCp/?repage";
	}
	
	@RequiresPermissions("bu:buOrderCp:edit")
	@RequestMapping(value = "delete")
	public String delete(BuOrderCp buOrderCp, RedirectAttributes redirectAttributes) {
		buOrderCpService.delete(buOrderCp);
		addMessage(redirectAttributes, "删除主子表成功");
		return "redirect:"+Global.getAdminPath()+"/bu/buOrderCp/?repage";
	}

}